package com.sgy.spring_ioc_xml_anno.service.impl;

import com.sgy.spring_ioc_xml_anno.dao.AccountDao;
import com.sgy.spring_ioc_xml_anno.pojo.Account;
import com.sgy.spring_ioc_xml_anno.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 * @date 2025/7/13 11:58
 * @description
 */
@Service("transferService")
public class TransferServiceImpl implements TransferService {

    // @Autowired 按照类型注入，如果按照类型无法唯一锁定对象，可以结合@Qualifier指定具体的id
    @Autowired
    @Qualifier("accountDao")
    private AccountDao accountDao;

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney()-money);
        to.setMoney(to.getMoney()+money);

        accountDao.updateAccountByCardNo(to);
        accountDao.updateAccountByCardNo(from);
    }
}
